<?php

ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\tools\Log;

$body = isset($argv[1]) ? $argv[1] : '';
setErrorHandler();
if ($body) {
    $body = base64_decode($body);
    $body = json_decode($body, true);
    try {
        $execFunction = json_decode($body['__q'], true);
        call_user_func($execFunction, $body);
    } catch (\Exception $e) {
        Log::warning(json_encode(['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()], JSON_UNESCAPED_UNICODE));
        exit(255);
    }
    exit;
} else {
    exit(2);
}

function setErrorHandler()
{
    set_error_handler(function ($errno, $errstr, $errfile, $errline) {
        switch ($errno) {
            case E_USER_ERROR:
                $errInfo = [
                    'errorno' => $errno,
                    'errorstr' => $errstr,
                    'errorfile' => $errfile,
                    'errorline' => $errline
                ];
                Log::error(json_encode(['error_info' => $errInfo], JSON_UNESCAPED_UNICODE));
                break;
        }
    });
}
